<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/19
 * Time: 下午11:27
 */

namespace backend\server;


use common\logic\MenuLogic;
use common\models\AuthRuleModel;
use common\server\BasicDataServer;
use common\server\Server;
use backend\components\MenuHelper;
use yii\helpers\ArrayHelper;

class MenuServer extends Server
{
    public static $firstMenu = '顶级菜单';


    /**
     * 获取菜单
     * @return array
     */
    public function getAllMenu()
    {
        $dataList = [];
        $all = MenuLogic::instance()->getBySort();
        if ($all) {
            $all = $this->_formatMenus($all);

            $dataList = ArrayHelper::map($all, 'id', 'title');
        }
        array_unshift($dataList, self::$firstMenu);

        return $dataList;
    }

    /**
     * 获取菜单名称
     *
     * @param $id
     * @return string
     */
    public function getMenuTitle($id)
    {
        if ($id == 0) {
            return self::$firstMenu;
        };

        $model = MenuLogic::instance()->getById($id);
        return empty($model) ? '' : $model->title;
    }

    /**
     * @param AuthRuleModel $data
     * @return string
     */
    public function getShow(AuthRuleModel $data)
    {
        return BasicDataServer::COMMON_SHOW[$data->is_show];
    }

    /**
     * 菜单状态
     *
     * @param AuthRuleModel $data
     * @return mixed
     */
    public function getStatus(AuthRuleModel $data)
    {
        return BasicDataServer::COMMON_STATUS[$data->status];
    }

    /**
     * @return array
     */
    public function getBreadcrumbs()
    {
        $route = \Yii::$app->controller->action->id;

        $route = MenuHelper::normalizeRoute($route);

        $breadcrumbs = [];

        $menu = MenuLogic::instance()->getByRoute($route);
        if (!empty($menu->title)) {
            $breadcrumbs[] = $menu->title;
            $this->getMenuParent($breadcrumbs, $menu);
        }
        return ['title' => !empty($menu->title) ? $menu->title : '', 'breadcrumbs' => $breadcrumbs];
    }

    protected function getMenuParent(array &$breadcrumbs, AuthRuleModel $menu = null)
    {
        if (!empty($menu) && $menu->pid > 0) {
            $menu = MenuLogic::instance()->getById($menu->pid);
            array_unshift($breadcrumbs, ['label' => $menu->title, 'url' => [empty($menu->route) ? 'index' : $menu->route]]);
            $this->getMenuParent($breadcrumbs, $menu);
        }
    }


    /**
     * @param AuthRuleModel[] $menus
     * @param int $parent_id
     * @param int $level
     * @return array
     */
    protected function _formatMenus($menus, $parent_id = 0, $level = 1)
    {
        $items = [];
        foreach ($menus as $key => $menu) {
            if ($menu->pid == $parent_id) {

                $title = $this->getStrPad(4 * $level) . $menu->title;
                $item = ['title' => $title, 'id' => $menu->id];
                unset($menus[$key]);

                $items[] = $item;
                $child = $this->_formatMenus($menus, $menu->id, $level + 1);

                if (!empty($child)) {
                    foreach ($child as $item) {
                        $items[] = $item;
                    }
                }
            }
        }
        return $items;
    }

    protected function getStrPad($len)
    {
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= ' ';
        }
        return $str;
    }

    public function getMenuToSetting($ids)
    {
        $all = MenuLogic::instance()->getBySort();
        $all = $this->_formatSettingMenu($all, $ids);
        return $all;
    }

    protected function _formatSettingMenu($menus, $ids, $parent_id = 0)
    {
        $items = [];
        foreach ($menus as $key => $menu) {
            if ($menu->pid == $parent_id) {
                $item = ['text' => $menu->title, 'id' => $menu->id];
                if (in_array($menu->id, $ids)) {
                    $item['state'] = ['checked' => true];
                }
                unset($menus[$key]);
                $child = $this->_formatSettingMenu($menus, $ids, $menu->id);
                if (!empty($child)) {
                    $item['nodes'] = $child;
                }
                $items[] = $item;
            }
        }
        return $items;
    }


    public function checkMenuIds($ids)
    {

    }

}